package com.andy.qhb.game;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import com.andy.qhb.dao.DaoUtils;
import com.andy.qhb.dao.vo.Where;
import com.andy.qhb.entity.UsrFriends;
import com.andy.qhb.entity.UsrGameInfo;
import com.andy.qhb.entity.UsrGroups;
import com.andy.qhb.entity.UsrMsgLog;
import com.andy.qhb.entity.UsrUser;
import com.andy.qhb.utils.GoConstant;
import com.andy.qhb.vo.HFriend;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

public class GameUser implements Serializable{

	private static final long serialVersionUID = 1L;

	private UsrUser user;
	private UsrGameInfo gameInfo;
	private UsrFriends friends;
	private List<HFriend> groups;
	private Long id;
	private Room room;
	private boolean miansi;
	private int msgIndx; // 加载消息指针
	
	private List<UsrMsgLog> rlogs; // 邀请列表
	
	public GameUser() {
	}
	
	public GameUser(UsrUser user) {
		this.setUser(user);
		this.id = user.getId();
	}
	
	public boolean inSession() {
		return gameInfo != null;
	}
	
	public String getUserName() {
		return user.getUserName();
	}

	public String getNickName() {
		return user.getNickName();
	}

	public UsrUser getUser() {
		return user;
	}

	public void setUser(UsrUser user) {
		this.user = user;
	}

	public UsrGameInfo getGameInfo() {
		return gameInfo;
	}

	public void setGameInfo(UsrGameInfo gameInfo) {
		this.gameInfo = gameInfo;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Map<String, Object> getUserInfo() {
		Map<String, Object> resu = Maps.newHashMap();
		resu.put("userId", id);
		resu.put("nickName", getNickName());
		resu.put("userName", getUserName());
		resu.put("icon", user.getIcon());
		resu.put("coin", gameInfo.getCoin());
		return resu;
	}
	
	public HFriend forOther() {
		return new HFriend(id,getNickName(),user.getIcon());
	}

	public boolean robot() {
		return user.getRole() > 0;
	}
	
	public boolean miansi() {
		return miansi || user.getRole() == 2;
	}
	
	public void miansi(boolean miansi) {
		this.miansi = miansi;
	}

	public UsrFriends getFriends() {
		return friends;
	}

	public void setFriends(UsrFriends friends) {
		this.friends = friends;
	}

	
	public List<HFriend> getGroups() {
		if(groups == null){
			groups = Lists.newArrayList();
			String key = id+",";
			for(UsrGroups gr:Global.getGroups()){
				if(gr.getGuids().indexOf(key) >= 0){
					groups.add(new HFriend(gr.getId(), gr.getName(),null));
				}
			}
		}
		return groups;
	}
	public void addGroup(UsrGroups gr) {
		groups.add(new HFriend(gr.getId(), gr.getName(),null));
	}

	public Room getRoom() {
		return room;
	}

	public void setRoom(Room room) {
		this.room = room;
	}

	public List<UsrMsgLog> rlogs(){
		if(rlogs == null) {
			rlogs = DaoUtils.getList(UsrMsgLog.class, Where.eq("uid_other", getId()),
					Where.eq("type", GoConstant.MSG_0),
					Where.eq("status", 0));
		}
		return rlogs;
	}
	
	public void addRlog(UsrMsgLog msgLog){
		rlogs().add(msgLog);
	}
	public UsrMsgLog getRlog(Long msgId){
		for(UsrMsgLog log:rlogs()){
			if(log.getId().equals(msgId)){
				return log;
			}
		}
		return null;
	}

	public int getMsgIndx() {
		return msgIndx;
	}

	public void setMsgIndx(int msgIndx) {
		this.msgIndx = msgIndx;
	}

	public String getIcon() {
		return user.getIcon();
	}

}
